<template>
  <el-dialog :title="$t('checkStock.create')" v-model="visible"
             :close-on-click-modal="false" draggable>
    <el-table :data="state.dataList" border
              :cell-style="tableStyle.cellStyle"
              @selection-change="handleSelectionChange"
              :header-cell-style="tableStyle.headerCellStyle"
               class="layout-padding-view">
      <el-table-column type="selection" width="40" align="center"/>
      <el-table-column type="index" label="#" width="60"/>
      <el-table-column prop="k3Code" label="k3仓库代码"/>
      <el-table-column prop="code" label="MES仓库代码"/>
    </el-table>
    <template #footer>
      <span class="dialog-footer">
        <el-button type="primary" @click="create"
                   :disabled="state.loading">{{ $t('common.confirmButtonText') }} </el-button>
        <el-button @click="visible = false">{{ $t('common.cancelButtonText') }}</el-button>

      </span>
    </template>
  </el-dialog>
</template>

<script setup lang="ts">
import {useI18n} from 'vue-i18n';
import {BasicTableProps, useTable} from "/@/hooks/table";
import {mainDgFunctions} from "/@/views/wms/checkStock/data"
import {useMessage} from "/@/hooks/message";
import {checkstockHCreate} from "/@/api/platform/mes/wmsCheckstock";

// 通知父组件刷新
const emit = defineEmits(['refresh']);
// 初始化
const {t} = useI18n()
const state: BasicTableProps = reactive<BasicTableProps>({
  isPage: false
})
const {
  tableStyle
} = useTable(state)
const {getWarehouseDataList, createInventory} = mainDgFunctions(t, state);

// 声明变量
const visible = ref(false); // 弹框是否显示
const multipleSelection = ref([]) // 表格选中行
// 初始化表格


// 页面加载完成后执行操作
onMounted(() => {
  // 从数据字典中获取仓库列表信息
})
// 打开弹框
const openDialog = () => {
  visible.value = true;
  getWarehouseDataList()
};

// 表格选中事件
const handleSelectionChange = (val) => {
  multipleSelection.value = val
};
// 创建盘点单
const create = () => {
  const rows = multipleSelection.value;
  if (!rows || rows.length === 0) {
    useMessage().warning(t("message.noChecked"))
    return
  }
  state.loading = true;
  // 提取所有的code值逗号分割
  const codes = rows.map(item => item.k3Code).join(",")
  checkstockHCreate({codes: codes}).then(res => {
    if (res?.code == 0) {
      useMessage().success(res.msg)
      visible.value = false
      emit('refresh')
    } else {
      useMessage().error(res?.msg || t("message.exception"));
    }
  }, error => {
    useMessage().error(error?.msg || t("message.exception"));
  }).finally(() => {
    state.loading = false
  })
}

// 暴露变量
defineExpose({
  openDialog
});
</script>
